window.uRowStatus = function(uid){
	var url = "ajax?r=userstate&uid="+uid;
	var method = "POST";
	$.ajax({
        type: method,
        url: url,
        beforeSend: function (xhr) {
        	$("div.user-state[uid="+uid+"]").attr("class","user-state userStatus-loading");
	  	},success: function(data) {
	  		if(data && data[0] && data[0]['error'] == 0)
		  		$("div.user-state[uid="+data[0]['data'][3]+"]").attr({"class":"user-state userStatus-"+data[0]['data'][1],"title":data[0]['data'][2]}).html('<span>'+data[0]['data'][0]+'</span>');
	  		else
				$("div.user-state[uid="+uid+"]").attr({"class":'user-state  userStatus-busy',"title":'Error: '+data[0]['data']});
	  	}
	});
}
window.uRow = function(uid,event){
	if(!$(event.toElement).hasClass('user-state')) window.userDialog(uid);
}
window.gRow = function(g){
	gid = $(g).attr('gid');
	$("#gid-link-"+gid).click();
}
window.updateUserstate = setInterval(function () {
	$("div.user-state").each(function(){
		var uid = $(this).attr('uid');
		var url = "ajax?r=userstate&uid="+uid;
		var method = "POST";
		$.ajax({
            type: method,
            url: url,
            beforeSend: function (xhr) {
            	//$("div.user-state[uid="+uid+"]").attr("class","user-state userStatus-loading");
		  	},success: function(data) {
		  		if(data && data[0] && data[0]['error'] == 0)
			  		$("div.user-state[uid="+data[0]['data'][3]+"]").attr({"class":"user-state userStatus-"+data[0]['data'][1],"title":data[0]['data'][2]}).html('<span>'+data[0]['data'][0]+'</span>');
		  		else if(data && data[0] && data[0]['error'] == 403){
		  			$("div.user-state[uid="+uid+"]").attr({"class":'user-state  userStatus-busy',"title":'Error: '+data[0]['data']});
		  			window.updateUserstate = clearInterval(window.updateUserstate);
		  		}else
					$("div.user-state[uid="+uid+"]").attr({"class":'user-state  userStatus-busy',"title":'Error: '+data[0]['data']});
		  	}
		});
	});
}, 10000);
function prepare_user(d){
    function rep(i,v){
        $("#user_"+i).html(v);
    }
    rep('id',d['id']);
    if($("a#user-edit-button"))
        $("a#user-edit-button").attr("href",'wizard?r=users&uid='+d['id']);
    if($("a#user-delete-button"))
        $("a#user-delete-button").attr("uid",d['id']);
    rep('fullname',d['fullname']+(d['active']=='1'?d['protected']=='1'?'<span style="display: inline-block;" class="ui-icon ui-icon-locked" title="Protected (Can NOT be Deleted or group changed)"></span>':'':' <span style="color:red">(Disabled)</span>'));
    rep('first',d['first']);
    rep('last',d['last']);
    rep('email',d['email']);
    rep('title',d['title']);
    rep('gender',d['gender_full']);
    rep('branch',d['branch_full']);
    rep('group',d['group_full']);
    rep('jobid',d['jobid']);
    //rep('lastupdate',d['lastupdate']);
    //rep('expires',d['expires']);
    //rep('country',d['country']);
    //rep('creator',d['creator']);
    //rep('editor',d['editor']);
    //rep('timeleft',(d['timeleft']&&d['timeleft'].length>3?d['timeleft']:'<span style="color:red">Expired!</span>'));
    //rep('subject',d['subject']+(d['active'] == '1'?'':' <span style="color:red">(Disabled)</span>'));
    //rep('text',d['text']);
    //if(d['to']!='') rep('notes',d['notes']);
    rep('notes',d['notes']);
    $("#user_loading").hide();
    $("#user_body").show();
}
window.userDialog = function(uid){
    $("#dialog_user").dialog("open");
    $("#user_body").hide();
    $("#user_loading").show();
    window.ajax = $.get('ajax?r=GET_USER&uid='+uid,function(data){
        if((data[0]['data'] === undefined) || ($.type(data) == "string")){
                $("#dialog_user").dialog("close");
                var data = new Array();
                data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
            }
            $.each(data, function(index, value) {
                if(value['error']){
                    $("#dialog_user").dialog("close");
                    console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
                    insertStickyError(value['data'],value['error']);
                }else{
                    prepare_user(value['data']);
                }
            });
    });
}
$("a#user-delete-button").click(function(){
    var uid = $(this).attr('uid');
    $("#dialog_user").dialog("close");
    $("#dialog_delete h1#delete-header").html("User Account Deleting");
    $("#dialog_delete p#delete-body").html("Are you sure you want to delete User Account#"+uid+"<br /><i style='color:red'>"+"!!!Warning: This process can't be reverted!!!"+"</li>");
    $("#dialog_delete button.delete_confirm").attr('uid',uid);
    $("#dialog_delete").dialog("open");
});
$("button.delete_confirm").click(function(){
    var uid = $(this).attr('uid');
    $("#dialog_delete").dialog("close");
    window.ajax = $.get('ajax?r=DEL_UID&tid='+uid,function(data){
        window.data = data;
        if((data[0]['data'] === undefined) || ($.type(data) == "string")){
                var data = new Array();
                data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
            }
        $.each(data, function(index, value) {
            if(value['error']){
                console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
                insertStickyError(value['data'],value['error']);
            }else{
                insertAutoSuccess(value['data']);
                $("tr[uid="+uid+"]").children('td, th')
                .animate({ padding: 0 })
                .wrapInner('<div />')
                .children()
                .slideUp(function() { $(this).closest('tr').remove(); });
            }
        });
    });
    $("#dialog_delete a#delete_confirm").attr('uid','');
})